//
// Created by uncleyang on 9/12/21.
//

#ifndef LOGINSERVER_BUFFER_H
#define LOGINSERVER_BUFFER_H


#include <vector>
#include <string>

class Buffer {
    friend class TcpConnection;
public:
    Buffer():
        readIndex(0),
        writeIndex(0),
        data(1024)
    {

    }
    ~Buffer() = default;

    bool IsEmpty(){
        return readIndex==writeIndex;
    }

    int PeekN(int n, std::string &msg);

    int PopN(int n);

    int Read(std::string &msg,int size);

    void Write(char* start, int len);


    size_t ReadableSize()const{
        return writeIndex-readIndex;
    }

    size_t WriteableSize() const{
        return data.size()-writeIndex;
    }

private:
    std::vector<char> data;
    size_t readIndex;
    size_t writeIndex;
};


#endif //LOGINSERVER_BUFFER_H
